package com.hymjweb.demo.hymj.system_maintenance.user.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.system_maintenance.user.bean.XtUserCachePojo;
import com.hymjweb.demo.hymj.system_maintenance.user.bean.XtUserPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface XtUserMapper {

    class XtUserMapperProvider {

        public String findCacheAll(Map map) {
            return new SQL() {{
                SELECT("xu.*");
                FROM("xt_user xu");
                WHERE("1=1");
            }}.toString();
        }

        public String getUserPojoByMobile(String mobile){
            return new SQL() {{
                SELECT("xu.*");
                FROM("xt_user xu");
                WHERE("1=1").AND().WHERE("xu.mobile = #{mobile}");
            }}.toString();
        }

        public String findAll(Map map) {
            String sql = new SQL() {{
                SELECT("xu.*");
                FROM("xt_user xu");
                WHERE("1=1 ");
            }}.toString();

            if (map != null) {
                /**
                 * keyName  keyDepartmentId  keyWorkingState  keyIsLock  keyRegisterStartDate  keyRegisterEndDate
                 */
                String keyName = DataTypeUtil.toStringObject(map.get("keyName")); // 姓名
                String keyDepartmentId = DataTypeUtil.toStringObject(map.get("keyDepartmentId")); // 部门ID
                Long keyWorkingStateId = DataTypeUtil.toLongObject(map.get("keyWorkingStateId")); // 在职状态
                Long keyIsLockId = DataTypeUtil.toLongObject(map.get("keyIsLockId")); // 锁定状态
                String keyRegisterStartDate = DataTypeUtil.toStringObject(map.get("keyRegisterStartDate")); // 入职开始日期
                String keyRegisterEndDate = DataTypeUtil.toStringObject(map.get("keyRegisterEndDate")); // 入职结束日期

                // 姓名、姓名拼音、首字母、电话号码
                if (keyName != null) {
                    sql += " and (xu.name like " + DataTypeUtil.quoteAllLikeStr(keyName)
                            + " or xu.full_pinyin like " + DataTypeUtil.quoteAllLikeStr(keyName)
                            + " or xu.camel_char_pinyin like" + DataTypeUtil.quoteAllLikeStr(keyName)
                            + " or xu.mobile like " + DataTypeUtil.quoteAllLikeStr(keyName) + ") ";
                }
                if (keyDepartmentId != null) {
                    sql += " and xu.department_id = " + DataTypeUtil.quoteStr(keyDepartmentId);
                }

                if (keyWorkingStateId != null) {
                    sql += " and xu.working_state_id = " + keyWorkingStateId;
                }

                if (keyIsLockId != null) {
                    sql += " and xu.is_lock_id = " + keyIsLockId;
                }

                if(keyRegisterStartDate!=null){
                    if(keyRegisterEndDate!=null){
                        int d=keyRegisterStartDate.compareTo(keyRegisterEndDate);
                        if(d>0){
                            String tempDate = keyRegisterStartDate;
                            keyRegisterStartDate = keyRegisterEndDate;
                            keyRegisterEndDate = tempDate;
                        }
                        sql += " and (date_format(xu.register_date,'%Y-%m-%d') between " + DataTypeUtil.quoteStr(keyRegisterStartDate) +" and " + DataTypeUtil.quoteStr(keyRegisterEndDate);
                    }
                    else{
                        sql += " and date_format(xu.register_date,'%Y-%m-%d') >= " + keyRegisterStartDate;
                    }
                }
                else{
                    if(keyRegisterEndDate!=null){
                        sql += " and date_format(xu.register_date,'%Y-%m-%d') <= " + keyRegisterEndDate;
                    }
                }
            }
            return sql;
        }

        public String findObjectById(String id) {
            String sql = new SQL() {{
                SELECT("xu.*");
                FROM("xt_user xu");
                WHERE("xu.id = #{id}");
            }}.toString();
            return sql;
        }

        public String insert(XtUserPojo obj) {
            String sql = new SQL() {{
                INSERT_INTO("xt_user");
                VALUES("id", "#{id}");
                VALUES("password", "#{password}");
                VALUES("salt", "#{salt}");
                VALUES("name", "#{name}");
                VALUES("full_pinyin", "#{fullPinyin}");
                VALUES("camel_char_pinyin", "#{camelCharPinyin}");
                VALUES("mobile", "#{mobile}");
                VALUES("id_card", "#{idCard}");
                VALUES("gender_id", "#{genderId}");
                VALUES("address", "#{address}");
                VALUES("email_address", "#{emailAddress}");
                VALUES("qq", "#{qq}");
                VALUES("wx", "#{wx}");
                VALUES("department_id", "#{departmentId}");
                VALUES("register_date", "#{registerDate}");
                VALUES("leave_date", "#{leaveDate}");
                VALUES("working_state_id", "#{workingStateId}");
                VALUES("is_lock_id", "#{isLockId}");
                VALUES("comment", "#{comment}");
                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }

        public String update(XtUserPojo obj) {
            return new SQL() {{
                UPDATE("xt_user xu");
                SET("xu.password = #{password}");
                SET("xu.salt = #{salt}");
                SET("xu.name = #{name}");
                SET("xu.full_pinyin = #{fullPinyin}");
                SET("xu.camel_char_pinyin = #{camelCharPinyin}");
                SET("xu.mobile = #{mobile}");
                SET("xu.id_card = #{idCard}");
                SET("xu.gender_id = #{genderId}");
                SET("xu.address = #{address}");
                SET("xu.email_address = #{emailAddress}");
                SET("xu.qq = #{qq}");
                SET("xu.wx = #{wx}");
                SET("xu.department_id = #{departmentId}");
                SET("xu.register_date = #{registerDate}");
                SET("xu.leave_date = #{leaveDate}");
                SET("xu.working_state_id = #{workingStateId}");
                SET("xu.is_lock_id = #{isLockId}");
                SET("xu.comment = #{comment}");
                SET("xu.update_time= #{updateTime}");
                SET("xu.update_user_id= #{updateUserId}");
                WHERE("xu.id = #{id}");
            }}.toString();
        }

        public String resetPassword(String loginCzyId, String generatePwdEncrypt, String generateSalt, Date updateTime,String updateUserId){
            return new SQL() {{
                UPDATE("xt_user xu");
                SET("xu.password = #{generatePwdEncrypt}");
                SET("xu.salt = #{generateSalt}");
                SET("xu.update_time= #{updateTime}");
                SET("xu.update_user_id= #{updateUserId}");
                WHERE("xu.id = #{loginCzyId}");
            }}.toString();
        }

        public String delete(String id) {
            String sql = new SQL() {{
                DELETE_FROM("xt_user");
                WHERE("id = #{id}");
            }}.toString();
            return sql;
        }

        public String findByDepartmentId(String departmentId) {
            return new SQL() {{
                SELECT("xu.id,xu.name,xu.mobile, xu.department_id");
                FROM("xt_user xu");
                WHERE("xu.department_id = #{departmentId}");
            }}.toString();
        }

        public String findUK(String mobile) {
            String sql = new SQL() {{
                    SELECT("xu.*");
                    FROM("xt_user xu");
                    WHERE("xu.mobile = #{mobile}");
                }}. toString();
              return sql;
         }

         public String queryCustomerSalesmanByKey(String key){
             String sql = new SQL() {{
                 SELECT("t.*");
                 FROM("xt_user t");
                 WHERE("t.id in (select b.salesman_id from b_customer b) and (t.name like " + DataTypeUtil.quoteAllLikeStr(key) +" or t.mobile like "+ DataTypeUtil.quoteAllLikeStr(key) +")");
             }}.toString();
             return sql;
         }
    }

    @Results(id = "xtUserMapperId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "password", property = "password"),
            @Result(column = "salt", property = "salt"),
            @Result(column = "name", property = "name"),
            @Result(column = "full_pinyin", property = "fullPinyin"),
            @Result(column = "camel_char_pinyin", property = "camelCharPinyin"),
            @Result(column = "mobile", property = "mobile"),
            @Result(column = "id_card", property = "idCard"),
            @Result(column = "gender_id", property = "genderId"),
            @Result(column = "address", property = "address"),
            @Result(column = "email_address", property = "emailAddress"),
            @Result(column = "qq", property = "qq"),
            @Result(column = "wx", property = "wx"),
            @Result(column = "department_id", property = "departmentId"),
            @Result(column = "register_date", property = "registerDate"),
            @Result(column = "leave_date", property = "leaveDate"),
            @Result(column = "working_state_id", property = "workingStateId"),
            @Result(column = "is_lock_id", property = "isLockId"),
            @Result(column = "comment", property = "comment"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "create_user_id", property = "createUserId"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "update_user_id", property = "updateUserId")
    })
    @SelectProvider(type = XtUserMapperProvider.class, method = "findAll")
    List<XtUserPojo> findAll(Map map);

    @ResultMap("xtUserMapperId")
    @SelectProvider(type = XtUserMapperProvider.class, method = "findCacheAll")
    List<XtUserCachePojo> findCacheAll(Map map);

    @ResultMap("xtUserMapperId")
    @SelectProvider(type = XtUserMapperProvider.class, method = "queryCustomerSalesmanByKey")
    List<XtUserPojo> queryCustomerSalesmanByKey(String key);

    @ResultMap(value = "xtUserMapperId")
    @SelectProvider(type = XtUserMapperProvider.class, method = "findObjectById")
    XtUserPojo findObjectById(@Param("id") String id);

    @ResultMap(value = "xtUserMapperId")
    @SelectProvider(type = XtUserMapperProvider.class, method = "getUserPojoByMobile")
    XtUserPojo getUserPojoByMobile(String username);

    @ResultMap(value = "xtUserMapperId")
    @SelectProvider(type = XtUserMapperProvider.class, method = "findByDepartmentId")
    List<XtUserPojo> findByDepartmentId(String departmentId);

    @ResultMap(value = "xtUserMapperId")
    @SelectProvider(type = XtUserMapperProvider.class, method = "findUK")
    XtUserPojo findUK(String mobile);

    @InsertProvider(type = XtUserMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(XtUserPojo obj);

    @UpdateProvider(type = XtUserMapperProvider.class, method = "update")
    int update(XtUserPojo obj);

    @UpdateProvider(type = XtUserMapperProvider.class, method = "resetPassword")
    int resetPassword(String loginCzyId, String generatePwdEncrypt, String generateSalt, Date updateTime,String updateUserId);

    @DeleteProvider(type = XtUserMapperProvider.class, method = "delete")
    int delete(String id);

}
